package com.shop.cereshop.business.service.after.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.business.dao.after.CereOrderAfterDAO;
import com.shop.cereshop.business.dao.distributor.CereDistributionOrderDAO;
import com.shop.cereshop.business.dao.distributor.CereShopCommissionRecordDao;
import com.shop.cereshop.business.dao.dl.CerePlatformUserPointDao;
import com.shop.cereshop.business.dao.order.CereOrderProductDAO;
import com.shop.cereshop.business.dao.order.CereShopOrderDAO;
import com.shop.cereshop.business.dl.logic.domain.CereShopCommissionRecord;
import com.shop.cereshop.business.dl.logic.domian.CerePlatformUserPoint;
import com.shop.cereshop.business.dl.logic.domian.CereShopTicketInfo;
import com.shop.cereshop.business.dl.logic.service.SendPostImpl;
import com.shop.cereshop.business.dl.logic.utils.JsonUtils;
import com.shop.cereshop.business.message.service.aliyun.AliyunMessageService;
import com.shop.cereshop.business.page.after.After;
import com.shop.cereshop.business.page.after.AfterHistory;
import com.shop.cereshop.business.page.after.Refund;
import com.shop.cereshop.business.page.order.Product;
import com.shop.cereshop.business.page.pay.PayLog;
import com.shop.cereshop.business.param.after.AfterGetAllParam;
import com.shop.cereshop.business.param.after.AfterIdParam;
import com.shop.cereshop.business.pay.weixin.service.WxPayService;
import com.shop.cereshop.business.redis.service.api.StringRedisService;
import com.shop.cereshop.business.service.after.CereOrderAfterService;
import com.shop.cereshop.business.service.log.CerePlatformLogService;
import com.shop.cereshop.business.service.order.CereOrderReconciliationService;
import com.shop.cereshop.business.service.order.CereShopOrderService;
import com.shop.cereshop.business.service.pay.CerePayLogService;
import com.shop.cereshop.business.service.product.CereProductSkuService;
import com.shop.cereshop.business.service.redis.CereRedisKeyServcice;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.constant.StringEnum;
import com.shop.cereshop.commons.constant.WxPayEnum;
import com.shop.cereshop.commons.domain.after.CereOrderAfter;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.distributor.CereDistributionOrder;
import com.shop.cereshop.commons.domain.order.CereOrderProduct;
import com.shop.cereshop.commons.domain.order.CereOrderReconciliation;
import com.shop.cereshop.commons.domain.order.CereShopOrder;
import com.shop.cereshop.commons.domain.pay.CerePayLog;
import com.shop.cereshop.commons.domain.product.CereProductSku;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CereOrderAfterServiceImpl implements CereOrderAfterService {

    @Value("${Detail.expenditure.Refund}")
    private String refund;

    @Autowired
    private CereDistributionOrderDAO cereDistributionOrderDAO;

    @Autowired
    private CereShopCommissionRecordDao cereShopCommissionRecordDao;

    @Autowired
    private CerePlatformUserPointDao cerePlatformUserPointMapper;

    @Autowired
    private CereOrderAfterDAO cereOrderAfterDAO;

    @Autowired
    private AliyunMessageService aliyunMessageService;

    @Autowired
    private SendPostImpl sendPost;

    @Autowired
    private CereOrderProductDAO cereOrderProductDAO;

    @Autowired
    private CereShopOrderDAO cereShopOrderDAO;

    @Autowired
    private CereShopOrderService cereShopOrderService;

    @Autowired
    private CerePlatformLogService cerePlatformLogService;

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private StringRedisService stringRedisService;

    @Autowired
    private CereRedisKeyServcice cereRedisKeyServcice;

    @Autowired
    private CereProductSkuService cereProductSkuService;

    @Autowired
    private CerePayLogService cerePayLogService;

    @Autowired
    private CereOrderReconciliationService cereOrderReconciliationService;

    @Override
    public Page getAll(AfterGetAllParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<After> list = cereOrderAfterDAO.getAll(param);
        for (After c : list) {
            List<Long> classifyIdByAfterId = cereOrderAfterDAO.getClassifyIdByAfterId(c.getAfterId());
            if (classifyIdByAfterId.size() > 0) {
                c.setClassifyId(classifyIdByAfterId.get(0));
            }
        }
        PageInfo<After> pageInfo = new PageInfo<>(list);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    public After getById(Long afterId) throws CoBusinessException {
        After after = cereOrderAfterDAO.getById(afterId);
        if (after != null) {
            //查询商品信息
            List<Product> products = cereOrderAfterDAO.findProducts(afterId);
            for (Product product : products) {
                if (product.getClassifyId() == 7) {
                    product.setTotal(product.getTotal().multiply(new BigDecimal(10)));
                    product.setProductPrice(product.getProductPrice().multiply(new BigDecimal(10)));
                }
            }
            if (!EmptyUtils.isEmpty(products)) {
                //封装规格属性数据
                products.forEach(product -> product.setSkuDetails(cereOrderAfterDAO.findSkuAttribute(product.getAfterProductId())));
            }
            //查询协商历史
            List<AfterHistory> histories = cereOrderAfterDAO.findHistories(afterId);
            if (!EmptyUtils.isEmpty(histories)) {
                histories.forEach(afterHistory -> {
                    //封装图片数组
                    afterHistory.setImages(EmptyUtils.getImages(afterHistory.getImage()));
                    if (afterHistory.getTitle().contains("买家发起了")) {
                        afterHistory.setReason(after.getExplain());
                    } else {
                        afterHistory.setReason(after.getReason());
                    }
                });
            }
            after.setAfterHistory(histories);
            after.setProducts(products);
        }
        return after;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void refundSuccess(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException, Exception {
        String time = TimeUtils.yyMMddHHmmss();
        //w因为积分商品不会产生多个byOrderId , 所以取第一个
        List<Integer> byOrderId = cereOrderProductDAO.getByOrderId(param.getOrderId());
        Integer id = byOrderId.get(0);
        if (id == 7) {
            CereShopOrder order = cereShopOrderDAO.getByid2(param.getOrderId());
            Refund refund = cereOrderAfterDAO.findOrderPay(param.getAfterId());
            System.out.println("参数为：" + order.getPartnerOrderId());
            //订单信息改成已取消
            cereShopOrderDAO.updateBypk(order.getOrderId());
            //同意退款申请,修改售后单状态为退款中
            CereOrderAfter cereOrderAfter = new CereOrderAfter();
            cereOrderAfter.setAfterId(param.getAfterId());
            cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_SUCCESS.getCode());
            cereOrderAfter.setUpdateTime(time);
            cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
            cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "仅退款同意退款申请", param.getAfterId(), time);
            CerePlatformUserPoint point = new CerePlatformUserPoint();
            point.setBuyerUserId(order.getBuyerUserId());
            BigDecimal refundPrice = refund.getPrice();
//            BigDecimal refundPrice = refund.getPrice().multiply(new BigDecimal(10));
            point.setPoints(refundPrice.intValue());
            //积分来源, 默认0
            point.setSourceType((short) 0);
            point.setMemo("积分退款");
            point.setTime(new Date());
            point.setProductId(order.getProductId());
            cerePlatformUserPointMapper.insert(point);
//            throw new CoBusinessException(CoReturnFormat.SUCCESS);
            return;
        }
        CereShopOrder order = cereShopOrderDAO.selectByPrimaryKey(param.getOrderId());

        //同意退款申请,修改售后单状态为退款中
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_STAY.getCode());
        cereOrderAfter.setUpdateTime(time);

        if (order.getPartnerOrderId() != null) {
            //..根据orderId查询订单商品信息表
            CereOrderProduct cereOrderProduct = cereOrderProductDAO.findByOrderId(order.getOrderId());
            //如果是有，说明是优惠券退款，把优惠券改为已使用状态

            if (order.getZxkState() == 0) {
                //..调用助销客退单接口
                String cancelOrder = sendPost.CancelOrder(order.getOrderFormid(), order.getPartnerOrderId(), null, cereOrderProduct.getNumber());
                Integer refundStatus = (Integer) JsonUtils.toMap(cancelOrder).get("refundStatus");
                System.out.println(refundStatus);
                //.如果状态等等于,则代表成功
                if (refundStatus == 1) {
                    //.订单审核成功，改变数据库的zxkState状态,让下次不再继续调用助销客接口
                    cereShopOrderDAO.updateZxkState(order.getOrderId());
                    CereShopTicketInfo cereShopTicketInfo = cereOrderAfterDAO.findCereShopTicketInfoByOrderId(order.getOrderId());
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("name", cereShopTicketInfo.getCustomer());
                    hashMap.put("code", order.getOrderFormid());
                    aliyunMessageService.sendNotice2(cereShopTicketInfo.getCustomerTel(), hashMap);
                } else if(refundStatus == 3){
                    //退款失败则修改状态,并返回
                    cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_ERRPR.getCode());
                    cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
                    return;
//                    throw new CoBusinessException("订单取消失败");
                } else{
                    //如果是退款中,就直接返回
                    cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
                    return;
                }
            }
        }

        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);

        //根据售后单id查询订单支付信息和订单总金额
        Refund refund = cereOrderAfterDAO.findOrderPay(param.getAfterId());
        //调用退款接口
        if (refund != null) {
            //更新支付流水after字段
            CerePayLog cerePayLog = new CerePayLog();
            cerePayLog.setId(refund.getId());
            if (!EmptyUtils.isEmpty(refund.getAfter())) {
                //不是第一次退款,重新设置退款单号
                if (refund.getOutTradeNo().contains("XCX")) {
                    //小程序支付
                    refund.setOutRefundNo("XCXTK" + TimeUtils.todayTime2() + refund.getOrderFormid());
                } else if (refund.getOutTradeNo().contains("APP")) {
                    //APP支付
                    refund.setOutRefundNo("APPTK" + TimeUtils.todayTime2() + refund.getOrderFormid());
                }
                cerePayLog.setAfter(refund.getAfter() + "," + param.getAfterId());
            } else {
                cerePayLog.setAfter(String.valueOf(param.getAfterId()));
            }
            cerePayLogService.update(cerePayLog);
//            handleWxLogTest(refund.getOrderFormid(),refund.getTransactionId(),refund.getOutTradeNo(),param.getAfterId());
            Long payLogId = cerePayLog.getId();
            Map<String, String> resultMap = wxPayService.refund(refund.getTransactionId(), refund.getOutRefundNo(), refund.getOrderPrice(), refund.getPrice(),payLogId);
            if (!EmptyUtils.isEmpty(resultMap)) {
                if (resultMap.get("return_msg").equals(WxPayEnum.REFUND_SUCCESS.getCode()) &&
                        resultMap.get("return_code").equals(WxPayEnum.REFUND_OK.getCode())) {
                    //退款成功
                } else if (resultMap.get("return_code").equals(WxPayEnum.BUSINESS_BALANCE_NOTENOUGH.getCode())) {
                    //退款失败,商户余额不足
                    throw new CoBusinessException(CoReturnFormat.BUSINESS_BALANCE_NOT);
                }
            }
        }
        //删除自动确认收货延时任务
        stringRedisService.delete(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        cereRedisKeyServcice.deleteByKey(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        //清空自动关闭售后延时任务和任务记录
        stringRedisService.delete(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        cereRedisKeyServcice.deleteByKey(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "仅退款同意退款申请", param.getAfterId(), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void refundRefuse(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException, Exception {
        String time = TimeUtils.yyMMddHHmmss();
        //拒绝退款申请,修改售后单状态为审核不通过
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_STAY_NO.getCode());
        cereOrderAfter.setReason(param.getReason());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        cereShopOrderDAO.updateType(param.getOrderId(),param.getSkuId());
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "仅退款拒绝退款申请", param.getAfterId(), time);
        //重新开启自动确认收货延时任务
        String end = cereRedisKeyServcice.findByKey(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        if (!EmptyUtils.isEmpty(end)) {
            stringRedisService.set(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId(), TimeUtils.getCountDownByTime(time, end));
        }
        //清空自动关闭售后延时任务和任务记录
        stringRedisService.delete(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        cereRedisKeyServcice.deleteByKey(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void success(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException {
        String time = TimeUtils.yyMMddHHmmss();
        //同意退货退款申请,修改售后单状态为审核通过
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_STAY_YES.getCode());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "退货退款同意申请", param.getAfterId(), time);
        //删除自动确认收货延时任务
        stringRedisService.delete(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        cereRedisKeyServcice.deleteByKey(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        //清空自动关闭售后延时任务和任务记录
        stringRedisService.delete(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        cereRedisKeyServcice.deleteByKey(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void refuse(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException, Exception {
        String time = TimeUtils.yyMMddHHmmss();
        //拒绝退货退款申请,修改售后单状态为审核不通过
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_STAY_NO.getCode());
        cereOrderAfter.setReason(param.getReason());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "退货退款拒绝申请,拒绝原因：" +
                param.getReason(), param.getAfterId(), time);
        //重新开启自动确认收货延时任务
        String end = cereRedisKeyServcice.findByKey(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        if (!EmptyUtils.isEmpty(end)) {
            stringRedisService.set(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId(), TimeUtils.getCountDownByTime(time, end));
        }
        //清空自动关闭售后延时任务和任务记录
        stringRedisService.delete(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        cereRedisKeyServcice.deleteByKey(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void confirmAndRefund(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException, Exception {
        //资金将退回买家的支付账户，售后单状态变为售后成功，买家端变为已退款
        String time = TimeUtils.yyMMddHHmmss();
        //确认收货且退款,修改售后单状态为退款中
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_STAY.getCode());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        //根据售后单id查询订单支付信息和订单总金额
        Refund refund = cereOrderAfterDAO.findOrderPay(param.getAfterId());
        //调用退款接口
        if (refund != null) {
            //测试使用=退款
//            handleWxLogTest(refund.getOrderFormid(),refund.getTransactionId(),refund.getOutTradeNo(),param.getAfterId());
            //更新支付流水售后单id字段
            CerePayLog payLog = new CerePayLog();
            payLog.setId(refund.getId());
            if (!EmptyUtils.isEmpty(refund.getAfter())) {
                //不是第一次退款,重新设置退款单号
                if (refund.getOutTradeNo().contains("XCX")) {
                    //小程序支付
                    refund.setOutRefundNo("XCXTK" + TimeUtils.todayTime2() + refund.getOrderFormid());
                } else if (refund.getOutTradeNo().contains("APP")) {
                    //APP支付
                    refund.setOutRefundNo("APPTK" + TimeUtils.todayTime2() + refund.getOrderFormid());
                }
                //拼接售后单id
                payLog.setAfter(refund.getAfter() + "," + param.getAfterId());
            } else {
                //第一次售后退款
                payLog.setAfter(String.valueOf(param.getAfterId()));
            }
            cerePayLogService.update(payLog);
            Long payLogId = payLog.getId();
            Map<String, String> resultMap = wxPayService.refund(refund.getTransactionId(), refund.getOutRefundNo(), refund.getOrderPrice(), refund.getPrice(),payLogId);
            if (!EmptyUtils.isEmpty(resultMap)) {
                if (resultMap.get("return_msg").equals(WxPayEnum.REFUND_SUCCESS.getCode()) &&
                        resultMap.get("return_code").equals(WxPayEnum.REFUND_OK.getCode())) {

                } else if (resultMap.get("return_code").equals(WxPayEnum.BUSINESS_BALANCE_NOTENOUGH.getCode())) {
                    //退款失败,商户余额不足
                    throw new CoBusinessException(CoReturnFormat.BUSINESS_BALANCE_NOT);
                }
            }
        }
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "退货退款确认收货且退款", param.getAfterId(), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void damaging(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException {
        String time = TimeUtils.yyMMddHHmmss();
        //货物有损拒绝退款,修改售后单状态改为退货完成,拒绝退款
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_RETURN_SUCCESS_NOT_REFUND.getCode());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        if (EmptyUtils.isEmpty(param.getReason())) {
            param.setReason("无");
        }
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "退货退款货物有损拒绝退款,拒绝原因：" +
                param.getReason(), param.getAfterId(), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void handleWxLog(String refundNo, String transaction_id, String orderNo) throws CoBusinessException {

        //w 票不能加入购物车, 所以只有票会进null, 其他都会有值
        Long payLogId = null;
        try {
            payLogId = Long.valueOf(refundNo.split("@@")[1]);
        } catch (NumberFormatException e) {
            payLogId = null;
        }
        String time = TimeUtils.yyMMddHHmmss();
        //根据退款单号查询支付流水
        PayLog payLog = cerePayLogService.findByTransactionId(transaction_id,payLogId);
        if (payLog != null) {
            Long afterId = null;
            //截取最新售后单id
            if (!EmptyUtils.isEmpty(payLog.getAfter())) {
                if (payLog.getAfter().contains(",")) {
                    String[] split = payLog.getAfter().split(",");
                    afterId = Long.parseLong(split[split.length - 1]);
                } else {
                    afterId = Long.parseLong(payLog.getAfter());
                }
            }
            //查询售后单数据
            CereOrderAfter cereOrderAfter = cereOrderAfterDAO.findById(afterId);
            if (cereOrderAfter != null) {
                CereShopOrder order = cereShopOrderDAO.getByid2(cereOrderAfter.getOrderId());
                if (order.getIsUse() != null) {
                    cereShopOrderDAO.updateByUse(order.getOrderId());
                }

                //...查询订单信息表
                CereDistributionOrder cereDistributionOrder = cereDistributionOrderDAO.getByorderId(payLog.getOrderId());
                if (cereDistributionOrder != null) {
                    //添加退款支出记录
                    CereShopCommissionRecord record = new CereShopCommissionRecord();
                    record.setCommission(cereDistributionOrder.getCommission().multiply(new BigDecimal(-1)));
                    record.setState(2);
                    record.setOrederId(cereDistributionOrder.getOrderId());
                    record.setDistributorId(cereDistributionOrder.getDistributorId());
                    record.setDetail(refund);
                    record.setCreateTime(time);
                    cereShopCommissionRecordDao.insertSelective(record);
                }
                //...根据orderid查询商品所在的分类
                Long classify99 = cereShopOrderDAO.getClassifyByOrderId(order.getOrderId());
                //...如果类型是优惠券, 改is_use=2
                if (classify99 != null && (classify99 == 8 || classify99 == 9)) {
                    cereShopOrderDAO.updateIsUseByOrderId(order.getOrderId());
                }

                //修改售后单状态为退款成功
                cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_SUCCESS.getCode());
                cereOrderAfter.setAfterId(afterId);
                cereOrderAfter.setUpdateTime(time);
                cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
                //查询订单是否有其他未退款商品(退款成功的商品才算退款商品)
                List<CereOrderProduct> list = cereOrderAfterDAO.findOtherProductsByAfterId(payLog.getOrderId(), afterId);
                CereShopOrder cereShopOrder = new CereShopOrder();
                cereShopOrder.setOrderId(payLog.getOrderId());
                cereShopOrder.setUpdateTime(time);
                List<String> typeList = cereShopOrderDAO.selByType(payLog.getOrderId());
                if (EmptyUtils.isEmpty(list) && typeList.size()==0) {
                    //如果没有其他未退款商品,订单整体商品售后成功，订单状态流转为已取消
                    cereShopOrder.setState(IntegerEnum.ORDER_STOP.getCode());
                }
                cereShopOrderService.updateState(cereShopOrder);
                //查询售后单商品数据,更新对应商品库存
                List<CereProductSku> skus = cereOrderAfterDAO.findAfterSkus(afterId);
                if (!EmptyUtils.isEmpty(skus)) {
                    skus.forEach(sku -> {
                        int stockNumber = cereProductSkuService.findStockNumber(sku.getSkuId());
                        if (!EmptyUtils.isEmpty(stringRedisService.get(String.valueOf(sku.getSkuId())))) {
                            stockNumber = (int) stringRedisService.get(String.valueOf(sku.getSkuId()));
                        }
                        //更新redis商品库存
                        stringRedisService.set(String.valueOf(sku.getSkuId()), stockNumber + sku.getStockNumber());
                    });
                    cereShopOrderService.updateBatchStock(skus);
                }

                //新增对账单数据
                CereOrderReconciliation reconciliation = new CereOrderReconciliation();
                reconciliation.setOrderId(cereOrderAfter.getOrderId());
                reconciliation.setMoney(cereOrderAfter.getPrice());
                reconciliation.setState(IntegerEnum.RELATIONSHIP_SOLVE_FROZEN.getCode());
                reconciliation.setType(IntegerEnum.RELATIONSHIP_ONCOME.getCode());
                reconciliation.setCreateTime(time);
                cereOrderReconciliationService.insert(reconciliation);
                //新增退款流水
                CerePayLog cerePayLog = new CerePayLog();
                cerePayLog.setCreateTime(time);
                cerePayLog.setOrderFormid(orderNo);
                cerePayLog.setOutTradeNo(orderNo);
                cerePayLog.setOutRefundNo(refundNo);
                cerePayLog.setTransactionId(transaction_id);
                cerePayLog.setRefundFee(cereOrderAfter.getPrice());
                cerePayLog.setPaymentMode(IntegerEnum.ORDER_PAY_WX.getCode());
                cerePayLog.setShopId(payLog.getShopId());
                cerePayLog.setState(StringEnum.PAY_LOG_REFUND.getCode());
                cerePayLog.setTotalFee(payLog.getTotalFee());
                cerePayLog.setUserId(payLog.getUserId());
                cerePayLog.setRemark(payLog.getOrderFormid() + "订单退款" + cereOrderAfter.getPrice() + "元");
                cerePayLogService.insert(cerePayLog);
            }
        }
    }

    public void handleWxLogTest(String orderFormid, String transaction_id, String orderNo, Long afterId) throws CoBusinessException {
        String time = TimeUtils.yyMMddHHmmss();
        //根据订单id查询支付流水
        PayLog payLog = cerePayLogService.findByOrderFormid(orderFormid);
        if (payLog != null) {
            //截取最新售后单id
            if (!EmptyUtils.isEmpty(payLog.getAfter())) {
                if (payLog.getAfter().contains(",")) {
                    String[] split = payLog.getAfter().split(",");
                    afterId = Long.parseLong(split[split.length - 1]);
                } else {
                    afterId = Long.parseLong(payLog.getAfter());
                }
            } else {
                //更新订单支付流水
                CerePayLog cerePayLog = new CerePayLog();
                cerePayLog.setId(payLog.getId());
                cerePayLog.setAfter(String.valueOf(afterId));
                cerePayLogService.update(cerePayLog);
            }
            //查询售后订单数据
            CereOrderAfter cereOrderAfter = cereOrderAfterDAO.findById(afterId);
            //修改售后单状态为退款成功
            if (cereOrderAfter != null) {
                cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_SUCCESS.getCode());
                cereOrderAfter.setAfterId(afterId);
                cereOrderAfter.setUpdateTime(time);
                cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
                //查询订单是否有其他未退款商品
                List<CereOrderProduct> list = cereOrderAfterDAO.findOtherProductsByAfterId(payLog.getOrderId(), afterId);
                CereShopOrder cereShopOrder = new CereShopOrder();
                cereShopOrder.setOrderId(payLog.getOrderId());
                cereShopOrder.setUpdateTime(time);
                if (EmptyUtils.isEmpty(list)) {
                    //如果没有其他未退款商品,订单整体商品售后成功，订单状态流转为已取消
                    cereShopOrder.setState(IntegerEnum.ORDER_STOP.getCode());
                }
                cereShopOrderService.updateState(cereShopOrder);
                //查询售后单商品数据,更新对应商品库存
                List<CereProductSku> skus = cereOrderAfterDAO.findAfterSkus(afterId);
                if (!EmptyUtils.isEmpty(skus)) {
                    skus.forEach(sku -> {
                        int stockNumber = cereProductSkuService.findStockNumber(sku.getSkuId());
                        if (!EmptyUtils.isEmpty(stringRedisService.get(String.valueOf(sku.getSkuId())))) {
                            stockNumber = (int) stringRedisService.get(String.valueOf(sku.getSkuId()));
                        }
                        //更新redis商品库存
                        stringRedisService.set(String.valueOf(sku.getSkuId()), stockNumber + sku.getStockNumber());
                    });
                    cereShopOrderService.updateBatchStock(skus);
                }
                //新增对账单数据
                CereOrderReconciliation reconciliation = new CereOrderReconciliation();
                reconciliation.setOrderId(cereOrderAfter.getOrderId());
                reconciliation.setMoney(cereOrderAfter.getPrice());
                reconciliation.setState(IntegerEnum.RELATIONSHIP_SOLVE_FROZEN.getCode());
                reconciliation.setType(IntegerEnum.RELATIONSHIP_ONCOME.getCode());
                reconciliation.setCreateTime(time);
                cereOrderReconciliationService.insert(reconciliation);
                //新增退款流水
                CerePayLog cerePayLog = new CerePayLog();
                cerePayLog.setCreateTime(time);
                cerePayLog.setOrderFormid(orderNo);
                cerePayLog.setOutTradeNo(orderNo);
                cerePayLog.setOutRefundNo(payLog.getOutRefundNo());
                cerePayLog.setTransactionId(transaction_id);
                cerePayLog.setRefundFee(cereOrderAfter.getPrice());
                cerePayLog.setPaymentMode(IntegerEnum.ORDER_PAY_WX.getCode());
                cerePayLog.setShopId(payLog.getShopId());
                cerePayLog.setState(StringEnum.PAY_LOG_REFUND.getCode());
                cerePayLog.setTotalFee(payLog.getTotalFee());
                cerePayLog.setUserId(payLog.getUserId());
                cerePayLog.setRemark(payLog.getOrderFormid() + "订单退款" + cereOrderAfter.getPrice() + "元");
                cerePayLogService.insert(cerePayLog);
            }
        }
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void refundError(String outRefundNo) throws CoBusinessException {
        String time = TimeUtils.yyMMddHHmmss();
        //根据订单id查询支付流水
        PayLog payLog = cerePayLogService.findByOutRefundNo(outRefundNo);
        if (payLog != null) {
            String afterId = "";
            //截取最新售后单id
            if (payLog.getAfter().contains(",")) {
                String[] split = payLog.getAfter().split(",");
                afterId = split[split.length - 1];
            } else {
                afterId = payLog.getAfter();
            }
            //修改售后单状态为退款失败
            CereOrderAfter cereOrderAfter = new CereOrderAfter();
            cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_ERRPR.getCode());
            cereOrderAfter.setAfterId(Long.parseLong(afterId));
            cereOrderAfter.setUpdateTime(time);
            cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        }
    }

    @Override
    public void pointsReturn(AfterIdParam param, CerePlatformBusiness user) throws CoBusinessException, Exception {
        String time = TimeUtils.yyMMddHHmmss();
        CereShopOrder order = cereShopOrderDAO.selectByPrimaryKey(param.getOrderId());
        System.out.println("参数为：" + order.getPartnerOrderId());
        //同意退款申请,修改售后单状态为退款中
        CereOrderAfter cereOrderAfter = new CereOrderAfter();
        cereOrderAfter.setAfterId(param.getAfterId());
        cereOrderAfter.setAfterState(IntegerEnum.AFTER_REFUND_STAY.getCode());
        cereOrderAfter.setUpdateTime(time);
        cereOrderAfterDAO.updateByPrimaryKeySelective(cereOrderAfter);
        //根据售后单id查询订单支付信息和订单总金额
        Refund refund = cereOrderAfterDAO.findOrderPay(param.getAfterId());
        //调用退款接口
        if (refund != null) {
            //...积分添加
            CerePlatformUserPoint point = new CerePlatformUserPoint();
            point.setBuyerUserId(order.getBuyerUserId());
            point.setPoints(refund.getPrice().intValue());
            //积分来源, 默认0
            point.setSourceType((short) 0);
            point.setMemo("积分兑换");
            point.setTime(new Date());
            point.setProductId(order.getProductId());
            cerePlatformUserPointMapper.insert(point);
        }

        //删除自动确认收货延时任务
        stringRedisService.delete(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        cereRedisKeyServcice.deleteByKey(StringEnum.ORDER_CONFIRM_DILEVERY.getCode() + "-" + param.getOrderId());
        //清空自动关闭售后延时任务和任务记录
        stringRedisService.delete(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        cereRedisKeyServcice.deleteByKey(StringEnum.AFTER_CANCEL.getCode() + "-" + param.getAfterId());
        //新增日志
        cerePlatformLogService.addLog(user, "售后管理", "商户端操作", "仅退款同意退款申请", param.getAfterId(), time);
    }

}
